package com.blog.oguzhan.soapui.utils.datasource;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;


public class RawFileDatasource extends Datasource<List<String>> {

	private String sourcePath;
	
	public RawFileDatasource(String name, String sourcePath) throws FileNotFoundException {
		super(name);
		this.sourcePath = sourcePath;
		readFromFile(sourcePath);
	}

	private void readFromFile(String sourcePath) throws FileNotFoundException {
		// READ FROM FILE AND CREATE A NEW RAW FILE DATASOURCE
		BufferedReader reader = new BufferedReader(new FileReader(new File(sourcePath)));
		List<String> lines = new LinkedList<String>();
		String line = null;
		try {
			while( (line = reader.readLine()) != null) {
				lines.add(line);
			}

			this.setData(lines);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public String toString() {
		return super.toString() + "@" + this.sourcePath;
	}
}
